home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / BreakLines.c < prev    next >
Text File  |  1994-07-29  |  3KB  |  136 lines

  1. /*
  2. BreakLines.c
  3.  
  4.     char *BreakLines(char *string,long lineLength);
  5. Takes a long C string and judiciously changes spaces to '\n' to yield lines
  6. shorter than lineLength, but words are never broken, even if they are longer
  7. than lineLength. All characters other than space and '\n' are treated as letters.
  8. E.g.    printf("%s\n",BreakLines(string,80));
  9.  
  10.     void PrintWrappedText(FILE *stream,const char *s);
  11. Prints out text, with word wrapping. Leaves the supplied string unmodified.
  12.  
  13.     void PrintWrappedTextToFile(const char *filename,const char *s);
  14. Appends wrapped text to a file.
  15.  
  16.     void PrintWrappedComment(FILE *stream,const char *s);
  17. Encloses the string s inside comment delimiters and prints it with word wrapping.
  18.  
  19.     void PrintWrappedCommentToFile(const char *filename,const char *s);
  20. Encloses the string s inside comment delimiters and appends it to a file,
  21. with word wrapping.
  22.  
  23.     The preprocessor symbol NEWLINE is normally defined as '\n' in VideoToolbox.h. 
  24. In the MATLAB environment it is instead defined as '\r' for reasons that are explained
  25. in VideoToolbox.h.
  26.  
  27. HISTORY:
  28. 1993    dgp    Wrote BreakLines.
  29. 9/7/93    dgp    Wrote PrintWrappedText and PrintWrappedTextToFile based on similar routines
  30.         called PrintComment, etc., written by dhb and jms.
  31. 9/11/93    dgp Added PrintWrappedComment
  32. */
  33. #include "VideoToolbox.h"
  34.  
  35. char *BreakLines(char *string,long lineLength)
  36. {
  37.     long i,leftMargin,rightMargin,length;
  38.     int here;
  39.     
  40.     leftMargin=0;
  41.     length=strlen(string);
  42.     while(1){
  43.         rightMargin=leftMargin+lineLength;
  44.         if(rightMargin>=length)return string;        /* successful completion */
  45.         here=0;
  46.         if(!here)for(i=leftMargin;i<rightMargin;i++)if(string[i]==NEWLINE){
  47.             here=1;
  48.             break;
  49.         }
  50.         if(!here)for(;i>=leftMargin;i--)if(string[i]==' ' || string[i]==NEWLINE){
  51.             here=1;
  52.             break;
  53.         }
  54.         if(!here)for(i=leftMargin;i<length;i++)if(string[i]==' ' || string[i]==NEWLINE){
  55.             here=1;
  56.             break;
  57.         }
  58.         if(!here)return string;
  59.         string[i]=NEWLINE;
  60.         leftMargin=i+1;
  61.     }
  62. }
  63.  
  64. /*
  65. ROUTINE: PrintWrappedText
  66. PURPOSE:
  67.   Prints out text, with word wrapping. Leaves the supplied string unmodified.
  68. */
  69.  
  70. void PrintWrappedText(FILE *stream,const char *s)
  71. {
  72.     char *sTemp;
  73.     
  74.     sTemp=malloc(strlen(s)+1L);
  75.     if(sTemp==NULL)PrintfExit("PrintWrappedText: malloc(%ld) failed." NL
  76.         ,strlen(s)+1L);
  77.     strcpy(sTemp,s);
  78.     BreakLines(sTemp,80);
  79.     fprintf(stream,"%s",sTemp);
  80.     free(sTemp);
  81. }
  82.  
  83. /*
  84. ROUTINE: PrintWrappedTextToFile
  85. PURPOSE:
  86.   Append wrapped text to a file.
  87. */
  88.  
  89. void PrintWrappedTextToFile(const char *filename,const char *s)
  90. {
  91.     FILE *stream;
  92.     
  93.     stream = fopen(filename,"a");
  94.     if (stream == NULL)
  95.         PrintfExit("PrintWrappedTextToFile: fopen(\"%s\",...) failed.",filename);
  96.     PrintWrappedText(stream,s);
  97.     fclose(stream);
  98. }
  99.  
  100. /*
  101. ROUTINE: PrintWrappedComment
  102. PURPOSE:
  103.   Prints out a comment, with word wrapping.
  104. */
  105.  
  106. void PrintWrappedComment(FILE *stream,const char *s)
  107. {
  108.     char *sTemp;
  109.     
  110.     sTemp=malloc(strlen(s)+6L);
  111.     if(sTemp==NULL)PrintfExit("PrintCommentString: malloc(%ld) failed." NL
  112.         ,strlen(s)+6L);
  113.     strcpy(sTemp,"/* ");
  114.     strcat(sTemp,s);
  115.     strcat(sTemp," */" NL);
  116.     PrintWrappedText(stream,sTemp);
  117.     free(sTemp);
  118. }
  119.  
  120. /*
  121. ROUTINE: PrintWrappedCommentToFile
  122. PURPOSE:
  123.   Append wrapped comment to a file.
  124. */
  125.  
  126. void PrintWrappedCommentToFile(const char *filename,const char *s)
  127. {
  128.     FILE *stream;
  129.     
  130.     stream = fopen(filename,"a");
  131.     if (stream == NULL)
  132.         PrintfExit("PrintWrappedCommentToFile: fopen(\"%s\",...) failed.",filename);
  133.     PrintWrappedComment(stream,s);
  134.     fclose(stream);
  135. }
  136.